function Actor(){
  this.name = null;
  this.uri = null;

  this.viewpoints = new Array();
};

Actor.prototype.toXml = function() {
  var xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
  xmlStr += "<actor ";
  if(this.uri != null && this.uri.trim() != "")
    xmlStr += "href=\"" + this.uri + "\" ";
  if(this.name != null && this.name.trim() != "")
    xmlStr += "name=\"" + this.name + "\" ";
  xmlStr += ">";

  for(i = 0; i < this.viewpoints.length; i++){
    var href, name, role;
    if ('undefined' == typeof this.viewpoints[i].href)
      href = "";
    else
      href = this.viewpoints[i].href + "";
    if ('undefined' == typeof this.viewpoints[i].name)
      name = "";
    else
      name = this.viewpoints[i].name + "";
    if ('undefined' == typeof this.viewpoints[i].role)
      role = "reader";
    else
      role = this.viewpoints[i].role + "";
    xmlStr += "<viewpoint href=\"" + href.toXml() + "\" role=\"" + role.toXml() + "\">" + name.toXml() + "</viewpoint>" ;
  }
  xmlStr += "</actor>";
  return xmlStr;
}

Actor.prototype.parseXml = function(xmlStr){
  var actor = new Actor();
  var xmlReader = Components.classes["@mozilla.org/saxparser/xmlreader;1"]
                          .createInstance(Components.interfaces.nsISAXXMLReader);
  xmlReader.contentHandler = {
    currentElement: null,
    tmpViewpoint: null,

    startDocument: function() {},
		endDocument: function() {},

    startElement: function(uri, localName, qName, /*nsISAXAttributes*/ attributes) {
      this.currentElement = qName;
      debug("[startElement] currentElement:" + this.currentElement ,1);

      if(qName == "actor")
        for(var i=0; i<attributes.length; i++) {
          if(attributes.getQName(i) == "name")
            actor.name = attributes.getValue(i);
          if(attributes.getQName(i) == "href")
            actor.uri = attributes.getValue(i);
        }
      if(qName == "viewpoint"){
        this.tmpViewpoint = {};
        for(var i=0; i<attributes.length; i++) {
          if(attributes.getQName(i) == "href")
            this.tmpViewpoint.href = attributes.getValue(i);
          if(attributes.getQName(i) == "name")
            this.tmpViewpoint.name = attributes.getValue(i);
          if(attributes.getQName(i) == "role")
            this.tmpViewpoint.role = attributes.getValue(i);
        }
      }
    },

    endElement: function(uri, localName, qName) {
      debug("[endElement] currentElement:" + this.currentElement + ", typeof:" + (typeof this.tmpViewpoint),1);
      if(this.currentElement == "viewpoint" && this.tmpViewpoint != null){
        actor.viewpoints.push(this.tmpViewpoint);
        this.tmpViewpoint = null;
        //debug("[endElement] vp.topics.push:" + vp.topics.length ,1);
      }
      this.currentElement = null;
    },

    characters: function(value) {
      debug("[characters] currentElement:" + this.currentElement + ", typeof:" + (typeof this.currentElement) + ", value:" + value,1);
      if((typeof this.currentElement) != "string")
        return;
      if(this.currentElement == "viewpoint"){
        this.tmpViewpoint.name = value;
        debug("[startElement] this.tmpViewpoint.name:" + this.tmpViewpoint.name ,1);
      }
    },
    processingInstruction: function(target, data) {},
    ignorableWhitespace: function(whitespace) {},
    startPrefixMapping: function(prefix, uri) {},
    endPrefixMapping: function(prefix) {},

    // nsISupports
    QueryInterface: function(iid) {
      if(!iid.equals(Components.interfaces.nsISupports) &&
         !iid.equals(Components.interfaces.nsISAXContentHandler))
        throw Components.results.NS_ERROR_NO_INTERFACE;
      return this;
    }
  }
  //var_dump("[actor.js::parserXml] actor:", actor, 4);
  xmlReader.parseFromString(xmlStr, "text/xml");
  //var_dump("[actor.js::parserXml] actor:", actor, 4);
  return actor;
}

Actor.prototype.httpGet = function(user,pass) {
	if(this.uri == null){
		return null;
	}
	var req = new customRequest('GET', this.uri, "");
	var http = new HTTPCore();
	if(typeof user == "string" && user != "")
	  http.auth(user, pass);

	var res = http.doOperation(req);
	if(!res) return false;
	debug("[actor.js] Actor.httpGet URL:" + this.uri, 1);
	debug("[actor.js] Actor.httpGet Response:" + http.response, 1);
	var res = this.parseXml(http.response);
	//var_dump("[actor.js::parserXml] res:", res, 4);
	return res;
}

Actor.prototype.httpPut = function(user, pass) {
	if(this.uri == null){
		return false;
	}
	var content = this.toXml();
  var req = new customRequest('PUT', this.uri, content);
  var http = new HTTPCore();
	if(typeof user == "string" && user != "")
	  http.auth(user, pass);
	debug("[actor.js::httpPut] request uri:" + this.uri, 1);
	debug("[actor.js::httpPut] request body:" + content, 1);
	var res = http.doOperation(req);
	debug("[actor.js::httpPut] Actor.httpPut URL:" + this.uri, 1);
	debug("[actor.js:httpPut] Response Header:" + http.status, 1);
	debug("[actor.js::httpPut] Actor.httpPut Response:" + http.response, 1);
	return res;
}